package gov.pbc.gx.nn.pz.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;

import gov.pbc.gx.nn.pz.entity.Account;
import gov.pbc.gx.nn.pz.entity.HcTcpjjhceqs;
import gov.pbc.gx.nn.pz.entity.HcpzXY;
import gov.pbc.gx.nn.pz.entity.PzFont;

@Repository
public class TcpjjhceqsDao extends BaseImageDao {

	private Integer PZ_CODE_ = 5;

	public TcpjjhceqsDao() {
		super();

	}

	public boolean isSameValue(HcTcpjjhceqs hcTcpjjhceqs) {
		String type = hcTcpjjhceqs.getType();
		String pzTemplate = hcTcpjjhceqs.getPzTemplate();
		String hql = " from HcTcpjjhceqs where deptId=:deptid and type=:type and pzTemplate=:pzTemplate and invalid=false ";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		map.put("type", type);
		map.put("pzTemplate", pzTemplate);
		List<HcTcpjjhceqs> hcTcpjjhceqsdatas = this.query(hql, map);
		if (!hcTcpjjhceqsdatas.isEmpty()) {
			return true;
		}
		return false;
	}

	public void saveTcpjjhceqs(HcTcpjjhceqs hcTcpjjhceqs) {
		if (!isSameValue(hcTcpjjhceqs)) {
			hcTcpjjhceqs.setId(null);
			hcTcpjjhceqs.setDeptId(this.getDeptId());
			this.save(hcTcpjjhceqs);
		} else {
			this.update(hcTcpjjhceqs);
		}
	}

	public HcTcpjjhceqs selectOneByTypeAndTemplate(String type,
			String pzTemplate) {
		List<HcTcpjjhceqs> hcTcpjjhceqss = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		map.put("pzTemplate", pzTemplate);
		if (type == null) {
			String hql = " from HcTcpjjhceqs where deptId=:deptid and type is null and pzTemplate=:pzTemplate and invalid=false";
			hcTcpjjhceqss = this.query(hql, map);
		} else {
			String hql = " from HcTcpjjhceqs where deptId=:deptid and type=:type and pzTemplate=:pzTemplate and invalid=false";
			map.put("type", type);
			hcTcpjjhceqss = this.query(hql, map);
		}
		if (!hcTcpjjhceqss.isEmpty()) {
			HcTcpjjhceqs tcp=hcTcpjjhceqss.get(0);
			tcp.setPzDate(new Date());
			return tcp;
		}
		return null;
	}

	public Collection<HcTcpjjhceqs> getCoByType(String type) {
		Collection<HcTcpjjhceqs> hcTcpjjhceqss;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		if (type == null) {
			String hql = " from HcTcpjjhceqs where deptId=:deptid and  invalid = false and type is null";

			hcTcpjjhceqss = this.query(hql, map);
		} else {
			String hql = " from HcTcpjjhceqs where deptId=:deptid and invalid = false and type=:type";
			map.put("type", type);
			hcTcpjjhceqss = this.query(hql, map);
		}
		for (HcTcpjjhceqs tcp : hcTcpjjhceqss) {
			tcp.setPzDate(new Date());
		}
		return hcTcpjjhceqss;
	}

	public Collection<HcTcpjjhceqs> getOneGroupByType() {
		String hql = " from HcTcpjjhceqs where deptId=:deptid and invalid = false group by type";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("deptid", this.getDeptId());
		Collection<HcTcpjjhceqs> hcTcpjjhceqss = this.query(hql, param);
		for (HcTcpjjhceqs tcp : hcTcpjjhceqss) {
			tcp.setPzDate(new Date());
		}
		return hcTcpjjhceqss;
	}

	public Collection<HcTcpjjhceqs> getMuByDeptName() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		String hql = "from HcTcpjjhceqs where deptId=:deptid and invalid=false order by pzTemplate";
		Collection<HcTcpjjhceqs> hcTcpjjhceqss = this.query(hql, map);
		for (HcTcpjjhceqs tcp : hcTcpjjhceqss) {
			tcp.setPzDate(new Date());
		}
		return hcTcpjjhceqss;

	}

	public List<HcpzXY> getHcTcpjjhceqsXYs(Integer pzcode) {
		List<HcpzXY> lists = getHcPzXYsBase(pzcode);
		return lists;
	}

	public void saveTcpjjhceqsXY(Collection<HcpzXY> hcTcpjjhceqsxys) {
		saveXjpzXYBase(hcTcpjjhceqsxys, PZ_CODE_);
	}

	public void updateFont(PzFont pzfont) {
		updateFontBase(pzfont, PZ_CODE_);
	}

	public void deleteTemplateById1(String id) {

		this.delete(id, HcTcpjjhceqs.class);
	}

	public String getQsNameByQsUsername(String qsUsername) {

		List<Account> ls = new ArrayList<Account>();
		String hql = "from Account where account=:qsUsername";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("qsUsername", qsUsername);
		ls = this.query(hql, map);
		if (ls != null) {
			for (Account a : ls) {
				if (a.getName() != null) {
					return a.getName();

				}
			}
		}

		return null;
	}

	public boolean updateTemplateNameDao(final String templatename,
			final String id) {

		// 根據id得到type
		String sql1 = "from HcTcpjjhceqs where id=:id and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		List<HcTcpjjhceqs> hcTcpjjhceqs = query(sql1, param);
		String typestr = hcTcpjjhceqs.get(0).getType();

		// 根據type查找所有的pztemplate是否用相同的，
		String sql = "from HcTcpjjhceqs where  invalid = false and type=:type ";
		Map<String, Object> param1 = new HashMap<String, Object>();
		param1.put("type", typestr);

		Collection<HcTcpjjhceqs> tcpjjhceqs = this.query(sql, param1);
		for (HcTcpjjhceqs tcp : tcpjjhceqs) {
			if (tcp.getPzTemplate().equals(templatename)) {
				return false;
			}
		}
		// 更新操作
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			String hql = "update HcTcpjjhceqs set pzTemplate=:pzTemplate  where id=:id ";

			@Override
			public Boolean doInSession(Session session) {
				Query query = session.createQuery(hql);
				query.setString("pzTemplate", templatename);
				query.setString("id", id);
				query.executeUpdate();
				return true;
			}

		});

	}

	public boolean updateTemplateTypeDao(final String templatetype,
			final String id) {
		// 判斷類型是否有相同的
		String sql = "select count(*) from HcTcpjjhceqs where type=:type and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("type", templatetype);
		int count = this.queryForInt(sql, param);
		if (count > 0) {
			return false;
		}
		// 更新類型
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			@Override
			public Boolean doInSession(Session session) {

				String sql = "from HcTcpjjhceqs where id=:id and invalid = false ";
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("id", id);
				List<HcTcpjjhceqs> hcTcpjjhceqs = query(sql, param);
				String typestr = hcTcpjjhceqs.get(0).getType();

				String sql1 = "from HcTcpjjhceqs where type=:type and invalid = false ";
				Map<String, Object> param1 = new HashMap<String, Object>();
				param1.put("type", typestr);
				List<HcTcpjjhceqs> tcpjjhceqs = query(sql1, param1);
				for (HcTcpjjhceqs tcp : tcpjjhceqs) {
					String hql2 = "update HcTcpjjhceqs set type=:type  where id=:id ";
					Query query = session.createQuery(hql2);
					query.setString("type", templatetype);
					query.setString("id", tcp.getId());
					query.executeUpdate();
				}

				return true;
			}

		});

	}

}
